package ${packageName}.modular.${modular}.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import ${packageName}.core.exception.BusinessException;
import ${packageName}.modular.${modular}.mapper.${className}Mapper;
import ${packageName}.modular.${modular}.pojo.${className};
import ${packageName}.modular.${modular}.service.${className}Service;

/**
*	${remark}
*/
@Service
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {

	
	// 查询所有
	@Override
	public IPage<${className}> list(Integer current, Integer limit) {

		IPage<${className}> page = new Page<${className}>(current, limit);

		LambdaQueryWrapper<${className}> queryWrapper = new LambdaQueryWrapper<${className}>();
		queryWrapper.orderByDesc(${className}::getSort).orderByDesc(${className}::getCreateTime);

		return this.page(page, queryWrapper);

	}

	// 添加或者保存
	@Override
	public Boolean insertOrUpdate(${className} ${modular}) {
		
		return this.saveOrUpdate(${modular});
	}

	// 删除
	@Override
	public Integer delete(String id) {
		return baseMapper.deleteById(id);
	}
	
	// 批量删除
	@Override
	public Integer batchDelete(List<${className}> list) {

		List<Integer> idList = list.stream().filter(e -> !ObjectUtils.isEmpty(e.getId())).map(e -> e.getId()).collect(Collectors.toList());
		this.removeByIds(idList);
		return list.size();
	}
	
	// 清空表数据
	@Override
	public void deleteAll() {

		baseMapper.cleanTable();
	}
	
	// 禁用 & 启用
	@Override
	public Integer status(String id, boolean status) {

		if (status) {
			return baseMapper.updateState(id, 1);
		} else {
			return baseMapper.updateState(id, 0);
		}
	}
	
	// 批量启用
	@Override
	public Integer online(List<${className}> list) {
	
		for (${className} bean : list) {
			baseMapper.updateState(String.valueOf(bean.getId()), 1);
		}
		return list.size();
	}

	// 批量禁用
	@Override
	public Integer offline(List<${className}> list) {
	
		for (${className} bean : list) {
			baseMapper.updateState(String.valueOf(bean.getId()), 0);
		}
		return list.size();
	}
}
